package com.gxa.admin.pojo;

import lombok.NoArgsConstructor;
import lombok.ToString;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;

/**
 * @author IKE <hp1041735092@outlook.jp>
 * @date 21/3/2024
 */
@ToString
@NoArgsConstructor
public class JwtEmp implements UserDetails {
    private Integer empId;
    private String empAccount;
    private String empPwd;
    private Boolean enabled;

    private Collection<? extends GrantedAuthority> authorities;  //权限信息

    /**
     * 将emp封装为jwtEmp,作为生成jwt的依据
     */
    public JwtEmp(Emp emp){
        empId = emp.getEmployeeId();
        empAccount = emp.getEmployeeAccount();
        empPwd = emp.getEmployeePwd();
        enabled = emp.getEmpStatus() == null ? true : emp.getEmpStatus();
        authorities = emp.getAllPermissions();
    }

    /**
     * 将当前用户信息返回给spring security管理
     */
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return this.authorities;
    }

    @Override
    public String getPassword() {
        return empPwd;
    }

    @Override
    public String getUsername() {
        return empAccount;
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return this.enabled;
    }
}
